﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using BostonAzureSite.ViewModels;
using BostonAzureSiteWebRole.Models;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace BostonAzureSiteWebRole.Controllers
{
    [HandleError]
    public class EventController : Controller
    {   
        public ActionResult Event()
        {
            return All();
        }

        public ActionResult All()
        {
            var imageList = new List<EventViewModel>();

            try
            {
                CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
                CloudBlobClient client;

                client = account.CreateCloudBlobClient();

                var container = client.GetContainerReference("firestarter");
                
                var blobs = container.ListBlobs(new BlobRequestOptions()
                {
                    UseFlatBlobListing = true,
                    BlobListingDetails = BlobListingDetails.All
                });

                
                int count = 0;

                foreach (var blobItem in blobs)
                {

                    if (blobItem.Uri.AbsolutePath.Contains("thumb"))
                        continue;

                    count++;

                    var evm = new EventViewModel(Path.GetFileName(blobItem.Uri.AbsolutePath), Path.GetFileNameWithoutExtension(blobItem.Uri.AbsolutePath) + "_thumb" + Path.GetExtension(blobItem.Uri.AbsolutePath));
                    imageList.Add(evm);

                    //lets only get 15 for now...page loading takes too long
                    if (count >= 15)
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Write(new LoggingException("Error retrieving firestarter blobs.", ex));
            }

            return View(imageList);
        }
    }
}
